<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>


	<div >
	 <ul id="needCategory" class="easyui-tree"></ul>
	</div>
	 <!-- <div >
	 <ul id="testCategory" class="easyui-tree"></ul>
	</div>
 -->
  
<!-- 设计需求的右键菜单 -->
<div id="contentCategoryMenu" class="easyui-menu" style="width:120px;" data-options="onClick:menuHandler">
    <div data-options="iconCls:'icon-add',name:'add'">添加</div>
    <div data-options="iconCls:'icon-remove',name:'rename'">重命名</div>
    <div class="menu-sep"></div>
    <div data-options="iconCls:'icon-remove',name:'delete'">删除</div>
</div>
<!--
<div id="contentCategoryMenu2" class="easyui-menu" style="width:120px;" data-options="onClick:menuHandler2">
    <div data-options="iconCls:'icon-add',name:'add'">添加</div>
    <div data-options="iconCls:'icon-remove',name:'rename'">重命名</div>
    <div class="menu-sep"></div>
    <div data-options="iconCls:'icon-remove',name:'delete'">删除</div>
</div> -->
<script type="text/javascript">
$(function(){
	$("#needCategory").tree({
		url : '/need/category/list',
		animate: true,
		method : "GET",
		dnd:true,
		onContextMenu: function(e,node){
            e.preventDefault();
            //console.log(node);
            $(this).tree('select',node.target);
            //console.log(node);
            $('#contentCategoryMenu').menu('show',{
                left: e.pageX,
                top: e.pageY
            });
        },
        onAfterEdit : function(node){
        	var _tree = $(this);
        	//性建的node的id默认是0，因此当id是0时
        	if(node.id == 0){
        		// 新增节点
        		$.post("/need/category/create",{parentId:node.parentId,name:node.text},function(data){
        			if(data.status == 200){
        				_tree.tree("update",{
            				target : node.target,
            				id : data.data.id
            			});
        			}else{
        				$.messager.alert('提示','创建'+node.text+' 分类失败!');
        			}
        		});
        	}else{
        		$.post("/test/category/update",{id:node.id,name:node.text});
        	}
        }
	});
});
function menuHandler(item){
	var tree = $("#needCategory");
	//var tree2 = $("#testCategory");
	var node = tree.tree("getSelected");
	//var node2 = tree2.tree("getSelected");
	//console.log(node.type);
	//如果选中的不是测试用例树
	if(item.name === "add"){
		if (!tree.tree("isLeaf",node.target)) {
			tree.tree('append', {
	            parent: (node?node.target:null),
	            data: [{
	                text: '新建分类',
	                id : 0,
	                parentId : node.id
	            }]
	        }); 
			var _node = tree.tree('find',0);
			tree.tree("select",_node.target).tree('beginEdit',_node.target);
		} else {
			
		}
		
	}else if(item.name === "rename"){
		tree.tree('beginEdit',node.target);
	}else if(item.name === "delete"){
		$.messager.confirm('确认','确定删除名为 '+node.text+' 的分类吗？',function(r){
			if(r){
				$.post("/need/category/delete/",{parentId:node.parentId,id:node.id},function(){
					tree.tree("remove",node.target);
				});	
			}
		});
	}
/* 	if (node2==null) {
	} else {
		if(item.name === "add"){
			console.log("选中add");
			tree2.tree('append', {
	            parent: (node2?node2.target:null),
	            data: [{
	                text: '新建分类',
	                id : 0,
	                parentId : node2.id
	            }]
	        }); 
			var _node = tree2.tree('find',0);
			tree2.tree("select",_node.target).tree('beginEdit',_node.target);
		}else if(item.name === "rename"){
			tree2.tree('beginEdit',node2.target);
		}else if(item.name === "delete"){
			$.messager.confirm('确认','确定删除名为 '+node2.text+' 的分类吗？',function(r){
				if(r){
					$.post("/test/category/delete/",{parentId:node2.parentId,id:node2.id},function(){
						tree2.tree("remove",node2.target);
					});	
				}
			});
		}
	} */
	
}

</script>